﻿using System;
using System.Text;

namespace Practice13
{
    public class Nums
    {
        private int[] array;
        private static readonly Random r = new Random();

        public Nums()
        {
            RefreshArray();
        }

        public void RefreshArray()
        {
            int len = r.Next(20, 31);
            array = new int[len];
            for (int i = 0; i < len; i++)
            {
                array[i] = r.Next(101);
            }
        }

        // 冒泡排序
        public void BubbleSort()
        {
            int len = array.Length;
            for (int i = 0; i < len - 1; i++)
            {
                for (int j = 0; j < len - i - 1; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
            }
        }

        public void SelectSort()
        {
            int len = array.Length;
            for (int i = 0; i < len - 1; i++)
            {
                int minIndex = i;
                for (int j = i + 1; j < len; j ++)
                {
                    if (array[j] < array[minIndex])
                    {
                        minIndex = j;
                    }
                }
                int temp = array[minIndex];
                array[minIndex] = array[i];
                array[i] = temp;
            }
        }

        public override string ToString()
        {
            if (array.Length == 0 || array == null)
            {
                return "[]";
            }
            StringBuilder sb = new();
            sb.Append("[");
            for (int i = 0; i < array.Length; i++)
            {
                sb.Append(array[i]);

                if (i != array.Length - 1)
                {
                    sb.Append(", ");
                }
            }
            sb.Append("]");
            return sb.ToString();
        }

        public static void Execute()
        {
            Nums n = new Nums();
            Console.WriteLine($"数组: {n.ToString()}");
            n.BubbleSort();
            Console.WriteLine($"数组: {n.ToString()}");
            n.RefreshArray();
            Console.WriteLine($"数组: {n.ToString()}");
            n.SelectSort();
            Console.WriteLine($"数组: {n.ToString()}");
        }
    }
}